<?php

namespace Admin\Model;

use Think\Model;

/**
 * Description of AdminModel
 *
 * @author xtceetg
 */
class PaylogModel extends Model {

    protected $tableName = "pay_log";

    public function getLoglist($order_num, $payid, $game_id, $area_num, $username, $dt1, $dt2, $firstRow, $listRows) {


        $sql = "select p.*,gur.refer from pay_log as p,game_user_relation as gur";
        $where = " where p.flag=1 and p.pay_uid=gur.uid and p.gid=gur.game_id and p.sid=gur.area_num";
        if ($order_num != '') {
            $where.=" and p.order_num='" . $order_num . "'";
        }
        if ($payid != 0) {
            $where.=" and p.paytype='" . $payid . "'";
        }
        if ($game_id != 0) {
            $where.=" and p.gid='" . $game_id . "'";
        }
        if ($area_num != 0) {
            $where.=" and p.sid='" . $area_num . "'";
        }
        if ($username != '') {
            $where.=" and p.pay_username='" . $username . "'";
        }
        if ($dt1 != '') {
            $where.=" and p.pay_time>='" . $dt1 . "'";
        }
        if ($dt2 != '') {
            $where.=" and p.pay_time<='" . $dt2 . "'";
        }
        $order = " order by p.order_num desc";
        $limit = " limit {$firstRow} , {$listRows}";
        if ($firstRow != '' || $listRows != '') {
            $query = $sql . $where . $order . $limit;
        } else {
            $query = $sql . $where . $order;
        }
        return $this->query($query);
    }

    public function getFormatList($list = array()) {
        $paylist = $this->field('pay_uid,pay_username,fee_cny,gid,sid')->where('flag=1')->select();
        for ($i = 0; $i < count($list); $i++) {
            for ($j = 0; $j < count($paylist); $j++) {
                if ($list[$i]['uid'] == $paylist[$j]['pay_uid'] && $list[$i]['username'] == $paylist[$j]['pay_username'] && $list[$i]['game_id'] == $paylist[$j]['gid'] && $list[$i]['area_num'] == $paylist[$j]['sid']) {
                    $list[$i]['money']+=floor($paylist[$j]['fee_cny']);
                }
            }
        }
        return $list;
    }

}
